package dk.mirasola.systemtraining.user.client.dataprovider;

import com.google.gwt.core.client.GWT;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import dk.mirasola.systemtraining.user.client.util.UserAsyncCallback;
import dk.mirasola.systemtraining.user.shared.service.BiddingSetDataProviderService;
import dk.mirasola.systemtraining.user.shared.service.BiddingSetDataProviderServiceAsync;
import dk.mirasola.systemtraining.user.shared.transfer.BiddingSetDetailsTO;
import dk.mirasola.systemtraining.user.shared.transfer.CreateBiddingSetTO;

import java.util.List;
import java.util.Set;

public class BiddingSetDetailsDataProvider extends AsyncDataProvider<BiddingSetDetailsTO> {
    private static final BiddingSetDataProviderServiceAsync service = GWT.create(BiddingSetDataProviderService.class);

    private int rowCount = 0;
    private int maxBiddingSets = 0;

    public BiddingSetDetailsDataProvider() {
        service.initialData(new UserAsyncCallback<int[]>() {
            @Override
            public void onSuccess(int[] result) {
                rowCount = result[0];
                maxBiddingSets = result[1];
                updateRowCount(rowCount, true);
            }
        });
    }

    @Override
    protected void onRangeChanged(final HasData<BiddingSetDetailsTO> display) {
        service.getBiddingSetDetails(display.getVisibleRange().getStart(), display.getVisibleRange().getLength(),
                new UserAsyncCallback<List<BiddingSetDetailsTO>>() {
                    @Override
                    public void onSuccess(List<BiddingSetDetailsTO> result) {
                        updateRowData(display, display.getVisibleRange().getStart(), result);
                    }
                });
    }

    public void createBiddingSet(final HasData<BiddingSetDetailsTO> creatingDisplay, CreateBiddingSetTO createBiddingSetTO) {
        service.createBiddingSetAndFirstBiddingSession(createBiddingSetTO,new UserAsyncCallback<Void>() {
            @Override
            public void onSuccess(Void v) {
                rowCount++;
                updateRowCount(rowCount,true);
                for (HasData<BiddingSetDetailsTO> display : getDataDisplays()) {
                    if (display == creatingDisplay) {
                        display.setVisibleRangeAndClearData(new Range(0,display.getVisibleRange().getLength()),true);
                    } else if (display.getVisibleRange().getStart() == 0) {
                        display.setVisibleRangeAndClearData(display.getVisibleRange(),true);                        
                    }
                }
            }
        });
    }

    public void deleteBiddingSets(final Set<String> biddingSetIds) {
        service.deleteBiddingSets(biddingSetIds, new UserAsyncCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                rowCount -= biddingSetIds.size();
                updateRowCount(rowCount,true);
                for (HasData<BiddingSetDetailsTO> display : getDataDisplays()) {
                    display.setVisibleRangeAndClearData(display.getVisibleRange(),true);
                }
            }
        });
    }

    public int getMaxBiddingSets() {
        return maxBiddingSets;
    }
}
